﻿using OnlineBookstore.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace OnlineBookstore.Controllers
{
    public class AdministrationInfoController : Controller
    {
        // GET: AdministrationInfo
        private BookcityEntities db = new BookcityEntities();

        public ActionResult GetAdmin()
		{
            var data = db.AdministrationInfo;
            return View(data.ToList());
		}



        [HttpPost]
        public ActionResult Login(AdministrationInfo administration)
        {
            var data = db.AdministrationInfo.Where(m => m.AdminLogIn == administration.AdminLogIn && m.Adminpassword == administration.Adminpassword).FirstOrDefault();
            if (data != null )
            {
                return Redirect("/ShopInfo/GetShopList");
            }
            var data2 = db.AdministrationInfo.Where(m => m.AdminLogIn == administration.AdminLogIn).FirstOrDefault();
            if (data2 == null)
            {
                ViewBag.LoginError = "用户名错误";
            }
            else
            {
                ViewBag.PasswordError = "密码错误";
            }
            return View();
        }
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }


        public ActionResult UpdateAdmin(int Id)
		{
            var data = db.AdministrationInfo.Where(m => m.Id == Id).FirstOrDefault();
            return View(data);
		}
        /// <summary>
        /// 修改账号信息
        /// </summary>
        /// <param name="administration"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateAdmin(AdministrationInfo administration)
        {
            if (ModelState.IsValid)
            {
                db.Entry(administration).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("GetAdmin");
            }
            return View();

        }
        /// <summary>
        /// 添加账号信息
        /// </summary>
        /// <param name="quaram"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddAdmin(AdministrationInfo admin)
        {
            // 验证手机号
            if (admin.AdminPhone == null || admin.AdminPhone.Length != 11)
            {
                ModelState.AddModelError("AdminPhone", "手机号必须为11位");
            }

            // 验证邮箱
            if (admin.AdminMall == null || admin.AdminMall.Length > 50) // 更合理的邮箱长度限制
            {
                ModelState.AddModelError("AdminMall", "邮箱长度不能超过50个字符");
            }
            else if (!System.Text.RegularExpressions.Regex.IsMatch(admin.AdminMall, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
            {
                ModelState.AddModelError("AdminMall", "请输入有效的邮箱格式");
            }

            if (ModelState.IsValid)
            {
                db.AdministrationInfo.Add(admin);
                db.SaveChanges();
                return RedirectToAction("GetAdmin");
            }

            // 验证失败时返回当前视图
            return View(admin); // 重要：返回带有用户输入数据的模型
        }

        [HttpGet]
        public ActionResult AddAdmin()
        {
            // 返回一个空模型
            return View(new AdministrationInfo());
        }

        public ActionResult DelAdmin(int? Id)
		{
            var data = db.AdministrationInfo.Where(m => m.Id == Id).FirstOrDefault();
			if (data == null)
			{
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            return View(data);

        }
        [HttpPost, ActionName("DelAdmin")]
        [ValidateAntiForgeryToken]
        public ActionResult DelAdmin(int Id)
		{
            var data = db.AdministrationInfo.Where(m => m.Id == Id).FirstOrDefault();
            db.AdministrationInfo.Remove(data);
            db.SaveChanges();
            return RedirectToAction("GetAdmin");
        }
    }
}